package com.alpha.service.config;

import com.alpha.core.http.AlphaHttpContext;
import com.alpha.core.utils.StringUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

/**
 * mybatis-plus 公共配置
 *
 * @author chenruwan
 * @since 2022-01-01
 */
@Configuration
@EnableTransactionManagement
public class MyBatisPlusConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 注册乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        // 分页插件
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        // 设置请求的页面大于最大页后操作，true跳回到首页，false继续请求，默认false
        paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认500条，-1不受限制
        paginationInterceptor.setMaxLimit(1000L);
        // 生成 countSql 优化掉 join, 现在只支持 left join
        paginationInterceptor.setOptimizeJoin(true);
        interceptor.addInnerInterceptor(paginationInterceptor);
        return interceptor;
    }

    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {
            public static final String CREATED_AT = "createdAt";
            public static final String UPDATED_AT = "updatedAt";
            public static final String CREATED_BY = "createdBy";
            public static final String UPDATED_BY = "updatedBy";

            @Override
            public void insertFill(MetaObject metaObject) {
                Date now = new Date();
                this.setFieldValByName(CREATED_AT, now, metaObject);
                this.setFieldValByName(UPDATED_AT, now, metaObject);

                String username = AlphaHttpContext.getUsername();
                if (StringUtil.isNotEmpty(username)) {
                    this.setFieldValByName(CREATED_BY, username, metaObject);
                    this.setFieldValByName(UPDATED_BY, username, metaObject);
                }
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                this.setFieldValByName(UPDATED_AT, new Date(), metaObject);

                String username = AlphaHttpContext.getUsername();
                if (StringUtil.isNotEmpty(username)) {
                    this.setFieldValByName(UPDATED_BY, username, metaObject);
                }
            }
        };
    }
}
